﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetCs.Common;
using AspNetCs.Bll;
using AspNetCs.Model;
using AspNetCs.Enumerations;
using AspNetCs.Enumerations.User_Enum;
using AspNetCs.Enumerations.TrueOrFalseEnum;
using System.Text.RegularExpressions;

public partial class Guest_Default : BasePage
{
    GuestBLL guestBLL = new GuestBLL();
    protected string WhereStr = " where Guestid<>0 ";
    protected void Page_Load(object sender, EventArgs e)
    {
        WhereStr += " and IsPassed=" + (int)IsTrueEnum.Yes;
        if (Base_GetLoginInfo.GetUserID > 0)
        {
            WhereStr += " or (IsUser=" + Base_GetLoginInfo.GetUserID + " and IsPrivate=" + (int)IsTrueEnum.Yes + ")";
        }

        if (!Page.IsPostBack)
        {
            dataBindGuestList();

            if (Base_GetLoginInfo.GetUserID > 0)
            {
                this.txtName.Text = Base_GetLoginInfo.GetUserName;
                this.txtEmail.Text = Base_GetLoginInfo.GetEmail;
                this.txtQQ.Text = Base_GetUserInfo.QQ;
                this.txtMsn.Text = Base_GetUserInfo.Msn;
                this.txtMyWeb.Text = Base_GetUserInfo.HomePage;
            }

            rbtnSexList.DataSource = EnumDescription.GetFieldTexts(typeof(Sex_Enum));
            rbtnSexList.DataTextField = "EnumDisplayText";
            rbtnSexList.DataValueField = "EnumValue";
            rbtnSexList.DataBind();
            rbtnSexList.SelectedIndex = 0;
        }
    }

    //绑定留言内容
    protected void dataBindGuestList()
    {
        DataPagesOperate pager = new DataPagesOperate();
        string cmdText = "select * from Guest " + WhereStr + "   order by IsUser desc,GuestTime desc";
        pager.BindRepeater(cmdText, AspNetPager1, "IsUser desc,GuestTime desc", "Guestid", rptGuestList);
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        dataBindGuestList();
    }

    protected void btnPublish_Click(object sender, EventArgs e)
    {
        addGuest();
    }


    /// <summary>
    /// 用户发表留言
    /// </summary>
    protected void addGuest()
    {
        Guest_Model model = new Guest_Model();

        model.GuestName = txtName.Text;
        model.GuestSex = Convert.ToInt32(rbtnSexList.SelectedValue);
        model.GuestQQ = txtQQ.Text;
        model.GuestMsn = txtMsn.Text;
        model.GuestEmail = txtEmail.Text;
        model.GuestWebPage = txtMyWeb.Text;
        model.GuestFeel = "";
        model.GuestHeadPortrait = "";
        model.GuestIP = GetClientIP();
        model.GuestTitle = txtTitle.Text;
        model.GuestContent = txtGuestContent.Text;
        model.GuestTime = DateTime.Now;
        if (Base_GetUserInfo.UserId == 0)
        {
            model.IsUser = 0;
        }
        else
        {
            model.IsUser = Base_GetLoginInfo.GetUserID;
        }

        model.IsPrivate = Convert.ToInt32(rbtnHiddenList.SelectedValue);
        model.IsPassed = 0;

        int count = guestBLL.addGuest(model);
        if (count > 0)
        {
            Response.Write("<script>alert('留言成功!');location.href='default.aspx'</script>");
            Response.End();
        }
        else
        {
            Response.Write("<script>alert('留言失败!');</script>");
        }
    }


    private string GetClientIP()
    {
        string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (null == result || result == String.Empty)
        {
            result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }

        if (null == result || result == String.Empty)
        {
            result = HttpContext.Current.Request.UserHostAddress;
        }
        return result;
    }
    protected void rptGuestList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

    }

    protected string UserImg(object num)
    {
        string Result = "";
        if (StringHelper.isNumber_int(num) == (int)Sex_Enum.boy)
        {
            Result = "<img src='" + configInfo.InstallDir + "images/01.gif' alt='男' width='80' height='90' >";

        }
        else if (StringHelper.isNumber_int(num) == (int)Sex_Enum.gril)
        {
            Result = "<img src='" + configInfo.InstallDir + "images/18.gif' alt='女'  width='80' height='90'>";
        }
        else
        {
            Result = "<img src='" + configInfo.InstallDir + "images/NoheaderImg.gif' alt='未知'  width='80' height='90'>";
        }



        return Result;
    }
    protected string AdminRe(string str, string Retime)
    {
        string Result = "";
        if (str != "")
        {
            Result += "<hr  size=\"1\" style='border:1px solid #B4C9E7;' /><span style='color:red;'>[管理员]</span>回复:" + Retime + "<br>";
            Result += str;
        }

        return Result;
    }
    protected string GuestUserName(object s, object ip)
    {
        Regex x = new Regex(@"(?<one>([0-9]{1,3}))\.(?<Two>([0-9]{1,3}))\.(?<three>([0-9]{1,3}))\.(?<Four>([0-9]{1,3}))");
        Match mc = x.Match(ip.ToString());

        string Result = "";
        try
        {
            if (s.ToString() == "")
            {
                Result = "【游客】<br>" + mc.Groups["one"].Value + ".*.*" + mc.Groups["Four"].Value;
            }
            else
            {
                Result = s.ToString();
            }
        }
        catch
        {
            Result = "";
        }
        return Result;
    }
}
